package com.monkeyboy;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Author Gavin
 * @date 2020.09.01 10:30
 */
public class test {
    public static void main(String[] args) throws Exception {
        //获取请求
        String url = "https://search.jd.com/Search?keyword=java";
        //解析网页(jsoup返回document就是document对象)
        Document document = Jsoup.parse(new URL(url), 30000);
        //所有再js中可以使用的方法都可以使用
        Element j_goodsList = document.getElementById("J_goodsList");
        //获取所有的li元素
        Elements lis = j_goodsList.getElementsByTag("li");
        //获取元素中的内容
        for (Element pre : lis) {
            String image = pre.getElementsByTag("img").eq(0).attr("src");
            String price = pre.getElementsByClass("p-price").eq(0).text();
            String title = pre.getElementsByClass("p-name").eq(0).text();
            System.out.println("=======================");
            System.out.println(image);
            System.out.println(price);
            System.out.println(title);
        }
    }
}
